Let’s start by creating a SQL Azure database and playing with it a bit. SQL Azure’s
signup process and development experience are slightly different from the
rest of the Windows Azure storage services.
1. Creating a Database
The first step is to go to http://sql.azure.com and provision an account. Note that
this is not the same URL as the one for the rest of the Windows Azure
services (which fall under http://windows.azure.com). During the process, you’ll be
asked to provide an administrator username and a password to log on to
your SQL Azure database instances. After creating an account, you should
see something similar to Figure 1.
There are several key elements to note in Figure 1. The DNS name fhdb1swfkd.database.windows.net (easy to
pronounce, isn’t it?) is unique to your account. You can think of it as
the equivalent of a “server,” and use it where you traditionally use SQL
Server’s “server” names—in connection strings, SSMS, and other tools.
However, remember that in this case, this DNS entry is just a façade,
and in reality your service stems from multiple machines, rather than
just one “server.” This façade is required to make the existing tools
and frameworks work against the cloud.
Figure 1 also shows
a list of databases. By default, you are allocated a master database. This plays a similar role as
in the SQL Server world. The next step is to create a database that you
can use to create tables and store actual data. To do that, click the
Create Database button. This brings up the dialog shown in Figure 2. This sample database is called
booktest, but you can obviously call
it whatever you want.
You can use this to create a new database and specify the size
limit of that database. Currently, SQL Azure supports databases of up to
10 GB in size. Note that the two sizes offered in the drop-down box (1
GB and 10 GB) differ in pricing, so be sure to check the prices for
each. The idea here is that, if you have larger data, you shard it by
creating multiple databases and spreading the data over them.
Once you’ve created the new database, it should show up as
indicated in Figure 3. This screen also shows
you the size of all your databases at any given time.
Note: You can also create databases through code or through the
command line at any time. For example, you can use the sqlcmd utility to create a database through
a command such as the following:SQLCMD -U [MyUsername]@[MyServername]
-P [MyPassword] -S
[MyServername].database.windows.net -d master
CREATE DATABASE [MyDatabaseName]
GO
2. Adding Firewall Rules
By default, SQL Azure blocks access to your services from any incoming
IP address. To change this, you must manually add firewall rules for all
the IP addresses from which you plan to access the service.
To look at the current set of firewall rules and to add/modify
rules, switch to the Firewall Settings tab, as shown in Figure 4. This lists all the rules you currently
have.
One important thing to note here is the checkbox at the top, which
asks whether you want to “Allow Microsoft Services access to this
server.” This is unchecked by default, which means that any application
hosted on Windows Azure cannot talk to your database on SQL Azure. If you plan to host
applications on Windows Azure that talk to SQL Azure, you must have this
checkbox checked. Don’t worry about your application on Windows Azure
being hosted in a different region/data center—SQL Azure does the right
networking magic to allow packets through.
Let’s add a firewall rule to allow access from the current
machine. Click the Add Rule button to bring up the dialog shown in Figure 5. If you already know the IP
address range of the machines from which you will access SQL Azure, you
can enter them here.
If you want to allow access from your current machine, use the IP
address displayed at the bottom as the range you want to open. This is
the external-facing IP address of your current machine, and is typically
dynamically assigned by your ISP. Note that, if this IP address changes,
you must modify this rule as well. Unfortunately, there’s no good way to
know when your ISP will throw you behind a different IP address. You
must constantly monitor access to SQL Azure, or use a website such as
http://whatismyipaddress.com to figure this
out.
At this point, you’re all set. You have a server and a database
provisioned. Let’s connect to it!
3. Using SQL Server Management Studio
The primary tool every user of SQL Server uses to connect to
and play with a database is SQL Server Management Studio (SSMS). Support
for SQL Azure is built into SSMS 2008 R2 (which was available as a CTP
as of this writing). Earlier versions will work against SQL Azure, but
you must enter queries manually, and most of the GUI integration won’t
work.
Open SSMS 2008 R2 to see the dialog shown in Figure 6.
Enter the DNS name from Figure 1. Switch the
authentication type to SQL Server Authentication and use the username
and password you picked when creating your SQL Azure account.
SSMS will connect to your database and open the Object Explorer,
as shown in Figure 7. You can
see both the master and the booktest databases you created earlier. If you
get a connection error at this stage, check that you’ve set up the right
firewall rules in SQL Azure to allow access to your external IP
address.
At this point, you can create tables as you normally would. Let’s
create a simple table. Execute the code shown in Example 1 using the query editor.
Example 1. Simple table creation
CREATE TABLE Employees(EmpID int primary key, Name varchar(30)) GO
|
If the query executes successfully, you should see the table
displayed in the Object Explorer, as shown in Figure 8.
You can now insert rows into the table and query from it, as shown
in Example 2 and Figure 9.
Example 2. Inserting and querying
INSERT INTO Employees (EmpID, Name) VALUES (1, 'Michael Scott'), (2, 'Jim Halpert'), (3, 'Dwight Schrute'), (4, 'Pam Halpert'), (5, 'Andy Bernard') GO
SELECT * FROM Employees GO
|
This is obviously a trivial example, but it does show that
standard SQL concepts and tools work as you would expect with SQL
Azure.
4. Using ADO.NET
Coding against SQL Azure is the same as coding against normal SQL Server.
The same ODBC/ADO.NET idioms work out of the box. The only difference
you should be mindful of here is that SQL Azure doesn’t support
switching database contexts. Your sessions must query against objects in
the same database for the lifetime of the connection.
Example 3 shows the same
listing from Figure 13-9, but querying
through ADO.NET using a C# application.
Example 3. Query table using ADO.NET
var connectionBuilder = new SqlConnectionStringBuilder();
connectionBuilder.DataSource = "fhdb1swfkd.database.windows.net";
connectionBuilder.InitialCatalog = "booktest";
connectionBuilder.Encrypt = true;
connectionBuilder.UserID = "sriramk";
connectionBuilder.Password = "<MyPassword>";
using(var con = new SqlConnection(connectionBuilder.ToString()))
{
con.Open();
using(var command = con.CreateCommand())
{
command.CommandText = "SELECT * FROM Employees";
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine("EmpID: {0}, Name:{1}",
reader["EmpID"].ToString(),
reader["Name"].ToString());
}
}
}
}